#!/bin/bash

rses=("XRD1" "XRD2" "XRD3")

activities=("Express" "Functional Test" "User Subscriptions")

lifetime=300

count=1

while :
do
    i=1
    for srcrse in ${rses[@]}; do
        filename=file${i}_$(uuidgen)
        ((i++))
        bs=$((1 + RANDOM % 10000))
        dd if=/dev/urandom of=$filename bs=$bs count=$count
        rucio upload --rse $srcrse --scope test $filename
        sleep 10
        rm $filename
        for dstrse in ${rses[@]}; do
            if [ $srcrse = $dstrse ]; then continue; fi
            if [ $((1 + RANDOM % 10)) = 1 ]; then
                path=$(rucio list-file-replicas test:$filename --pfns)
                gfal-rm $path
            fi
            index=$(($RANDOM % 3))
            rucio add-rule --activity "${activities[$index]}" --lifetime $lifetime --source-replica-expression $srcrse test:$filename 1 $dstrse
            sleep 10
        done
    done
done
